import { Controller, Get } from '@nestjs/common';
import { ExcelService } from './excel.service';
import {
  ApiBody,
  ApiConsumes,
  ApiOperation,
  ApiTags,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { Public } from '~/common/decorators/public.decorator';

@ApiTags('Tools - 表格模块')
@Controller('excel')
export class ExcelController {
  constructor(private readonly _ExcelService: ExcelService) {}

  @Get('test')
  @ApiOperation({ summary: '测试表格' })
  @Public()
  async exportExcel() {
    let url;
    try {
      // 示例数据
      const chineseHeaders = ['年龄', '姓名', '邮箱'];
      const dataList = [
        { name: 'John Doe', age: 30, email: 'john.doe@example.' },
        { name: 'Jane Smith', age: 25, email: 'jane.smith@example.' },
        { name: 'Sam Brown', age: 22, email: 'sam.brown@example.' },
      ];

      // 生成文件名
      const fileName = `最新的测试表格.xlsx`;

      // 排序数组
      const sortArr = ['age', 'name', 'email'];

      // 调用服务层方法
      url = await this._ExcelService.createStyledExcel(
        chineseHeaders,
        dataList,
        fileName,
        sortArr,
      );
    } catch (error) {
      console.log(error);
    }

    return { url };
  }

  @Get('template')
  @ApiOperation({ summary: '表格模板下载' })
  @Public()
  async downloadTemplate() {
    return this._ExcelService.downloadTemplate();
  }
}
